import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {
    //内部类
    public static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;//根节点
    }

    //对二叉树进行遍历
    //前序
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);

    }

    //以子问题的思路，用到返回值

    /*public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if (root == null) return ret;
        ret.add(root.val);
        List<Integer> leftTree = preorderTraversal(root.left);
        ret.addAll(leftTree);
        List<Integer> rightTree = preorderTraversal(root.right);
        ret.addAll(rightTree);
        return ret;
    }*/
    //中序
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    //后序
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        preOrder(root.right);
        preOrder(root.left);
        System.out.print(root.val + " ");
    }

    //获取树中节点的个数
    public int size(TreeNode root) {
        //遍历过程中，节点不为空，就可以++
        //左＋右+1
        if(root == null){
            return 0;
        }
        int ret = size(root.left) + size(root.right) + 1;
        return ret;
    }


    public static int usedSize = 0;
    public int size2(TreeNode root){
        if(root == null){
            return 0;
        }
        usedSize++;
        size2(root.left);
        size2(root.right);
        return usedSize;
    }

    //遍历
    public static int leafSize = 0;
    public int getLeafNodeCount1(TreeNode root){
        if (root == null){
            return 0;
        }
        if (root.left == null && root.right == null){
            leafSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
        return leafSize;
    }

    //左+右
    public int getLeafNodeCount2(TreeNode root){
        if (root == null){
            return 0;
        }
        if (root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right) ;

    }

    //左的k-1+右的k-1
    public int getKlevelNodeCount(TreeNode root,int k){
        if (root == null){
            return 0;
        }
        if (k == 1){
            return 1;
        }
        return  getKlevelNodeCount(root.left,k-1) +
                getKlevelNodeCount(root.right,k-1);
    }

    //获取二叉树的高度
    //左右的最大值 +1
    public static int getHight(TreeNode root){
        if (root == null){
            return 0;
        }
        int leftHight = getHight(root.left) ;
        int rightHight = getHight(root.right);
        return (leftHight>rightHight ? leftHight:rightHight)+1;
    }


    //检测value的元素在不在
    public TreeNode find(TreeNode root,int val){
        //先看根节点是不是要找的节点
        //看左边
        //看右边
        if (root == null){
            return null;
        }
        if (root.val == val){
            return root;
        }
        TreeNode leftL = find(root.left,val);
        if (leftL != null){
            return leftL;
        }//左边找到就不去右边了
        TreeNode leftR = find(root.left,val);
        if (leftR != null){
            return leftR;
        }
        return null;
    }

    //层序遍历
    public void levelOrder1(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode cur = root;
        if (root != null){
            queue.offer(root);
        }
        while (!queue.isEmpty()){
            TreeNode top = queue.poll();
            System.out.print(top.val+" ");
            if (top.left != null){
                queue.offer(top.left);
            }
            if (top.right != null){
                queue.offer(top.right);
            }
        }
    }

    /*public  List<List<Integer>> levelOrder(TreeNode root) {//返回值嵌套
        //每一层是一个List
        //如何区分第几层
        //每次队列不为空的时候，队列当中的节点就是当前层的节点，我们需要记录此时队列的大小
        List<List<Integer>> ret = new ArrayList<>();
        if (root == null){
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();//这一层节点的个数
            List<Integer> list = new ArrayList<>();
            while (size != 0){
                TreeNode top = queue.poll();
                //System.out.print(top.eval+" ");
                list.add(top.val);
                if (top.left != null) {
                    queue.offer(top.left);
                }
                if (top.right != null) {
                    queue.offer(top.right);
                }
                size--;
            }
            ret.add(list);
        }
        return ret;
    }*/

    public boolean isCompleteTree(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        if (root!=null){
            queue.offer(root);
        }

        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if (cur!= null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else{
                break;
            }
        }
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if (cur!= null){
                return false;
            }
        }
        return true;
    }


}











